package com.ljm.sys.auth.controller;

import com.ljm.common.dto.BaseResult;
import com.ljm.common.util.ResultUtil;
import com.ljm.sys.auth.api.SysAuthResourceApi;
import com.ljm.sys.auth.dto.SysAuthRoleResDto;
import com.ljm.sys.auth.dto.UserRefDto;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import com.ljm.sys.auth.api.SysAuthRoleApi;
import com.ljm.sys.auth.dto.SysAuthRoleDto;

import org.springframework.stereotype.Controller;

/**
 * <p>
    * 系统角色 前端控制器
    * </p>
 *
 * @author HuamingChen
 * @since 2020-06-11
 */
@Api("系统角色")
@RestController
@RequestMapping("/auth/sysAuthRole")
public class SysAuthRoleController {

    /**
    * 系统角色接口
    */
    @Resource
    private SysAuthRoleApi iSysAuthRoleApi;
    @Resource
    private SysAuthResourceApi iSysAuthResourceApi;

    @ApiOperation(value="保存系统角色")
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public BaseResult save(@ApiParam(value="系统角色") @RequestBody @Validated SysAuthRoleDto dto){
        iSysAuthRoleApi.saveData(dto);
        return ResultUtil.success();
    }

    @ApiOperation(value="删除系统角色")
    @RequestMapping(value = "/del",method = RequestMethod.GET)
    public BaseResult del(@ApiParam(value="主键") @RequestParam Long id){
        iSysAuthRoleApi.delData(id);
        return ResultUtil.success();
    }

    @ApiOperation(value="获取系统角色")
    @RequestMapping(value = "/getById",method = RequestMethod.GET)
    public BaseResult getById(@ApiParam(value="主键") @RequestParam Long id){
        return ResultUtil.successData(iSysAuthRoleApi.getDataById(id));
    }

    @ApiOperation(value="分页获取系统角色")
    @RequestMapping(value = "/getByPage",method = RequestMethod.GET)
    public BaseResult getByPage(@ApiParam(value="角色代码") @RequestParam(required = false) String roleCode,
                                @ApiParam(value="角色名称") @RequestParam(required = false) String roleName,
                                @ApiParam(value="页码") @RequestParam(defaultValue = "1") Integer pageNo,
                                @ApiParam(value="页大小") @RequestParam(defaultValue = "10") Integer pageSize){
        return ResultUtil.successData(iSysAuthRoleApi.getDataByPage(roleCode,roleName,pageNo,pageSize));
    }

    @ApiOperation(value="授权")
    @RequestMapping(value = "/setRoleRes",method = RequestMethod.POST)
    public BaseResult setRoleRes(@RequestBody SysAuthRoleResDto dto){
        iSysAuthRoleApi.setRoleRes(dto);
        return ResultUtil.success();
    }

    @ApiOperation(value="获取已授权")
    @RequestMapping(value = "/getRoleRes",method = RequestMethod.GET)
    public BaseResult getRoleRes(@RequestParam String roleCode){
        return ResultUtil.successData(iSysAuthRoleApi.getRoleRes(roleCode));
    }

    @ApiOperation(value="获取资源树")
    @RequestMapping(value = "/getResTree",method = RequestMethod.GET)
    public BaseResult getResTree(){
        return ResultUtil.successData(iSysAuthResourceApi.getResTree());
    }
}
